package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    public PageInfo queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.queryForList(qo));
    }

    public void update(StrategyDetail strategyDetail) {
        Date date = new Date();
        if (strategyDetail.getState() == StrategyDetail.STATE_RELEASE) {
            strategyDetail.setReleaseTime(date);
        }
        strategyDetailMapper.updateByPrimaryKey(strategyDetail);
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        strategyContent.setId(strategyDetail.getId());
        strategyContentMapper.updateByPrimaryKey(strategyContent);
    }

    public void save(StrategyDetail strategyDetail) {
        Date date = new Date();
        if (strategyDetail.getState() == StrategyDetail.STATE_RELEASE) {
            strategyDetail.setReleaseTime(date);
        }
        strategyDetail.setCreateTime(date);
        strategyDetailMapper.insert(strategyDetail);
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        strategyContent.setId(strategyDetail.getId());
        strategyContentMapper.insert(strategyContent);
    }

    public StrategyContent getContentById(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public StrategyDetail getByDetailId(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    //查询所有的分类数据
    public List<StrategyComment> getListStrategyComment() {
        return  strategyCommentMapper.selectAll();
    }
}
